package com.hjh.netty.elect.demo5.handler;

import com.hjh.netty.elect.demo5.bean.Pmc350DevPackage;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.handler.timeout.IdleStateHandler;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * 心跳处理: 连接的保存和释放
 *
 * @author 程就人生
 * @date 2020年8月6日
 * @Description
 */
@Slf4j
public class Pmc350DevHandler000 extends IdleStateHandler {

    private static final int READ_IDLE_GAP = 150;

    public Pmc350DevHandler000() {
        super(READ_IDLE_GAP, 0, 0, TimeUnit.SECONDS);
    }

    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        // 判断消息实例
        if (null == msg || !(msg instanceof Pmc350DevPackage)) {
            ctx.fireChannelRead(msg);
            return;
        }

        Pmc350DevPackage pmc350DevPackage = (Pmc350DevPackage) msg;

        if(pmc350DevPackage.getPackageType() != 0){
//            super.channelRead(ctx, msg);
            ctx.fireChannelRead(msg);
            return;
        }
        log.info("HeartBeat|{}",pmc350DevPackage);

        //channel的保存和释放
        ctx.flush();
    }

    @Override
    protected void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) throws Exception {
        //log.info(READ_IDLE_GAP + "秒内未读到数据!");
    }
}